<?php
 

/**
 * 按指定大小生成缩略图，而且不变形，缩略图函数
 *
 * @param $f unknown_type
 *       	 图片文件
 * @param $t unknown_type       	
 * @param $tw unknown_type
 *       	 生成图片的宽
 * @param $th unknown_type
 *       	 生成图片的高
 */
function makeThumb($image, $toFile, $toWidth, $toHeight) {
	$temp = array (1 => 'gif', 2 => 'jpeg', 3 => 'png' );
	
	list ( $fw, $fh, $tmp ) = getimagesize ( $image );
	if (! $temp [$tmp]) {
		return false;
	}
	$tmp = $temp [$tmp];
	$infunc = "imagecreatefrom$tmp";
	$outfunc = "image$tmp";
	
	$fimg = $infunc ( $image );
	$tempFw = $fw;
	$tempFh = $fh;
	
	if ($toWidth > $fw) {
		$toWidth = $tempFw;
	}
	if ($toHeight > $fw) {
		$toHeight = $tempFh;
	}
	
	if ($fw / $toWidth > $fh / $toHeight) {
		$fw = $toWidth * ($fh / $toHeight);
	} else {
		$fh = $toHeight * ($fw / $toWidth);
	}
	
	$timg = imagecreatetruecolor ( $toWidth, $toHeight );
	imagecopyresampled ( $timg, $fimg, 0, 0, 0, 0, $toWidth, $toHeight, $fw, $fh );
	
	if ($outfunc ( $timg, $toFile )) {
		return true;
	} else {
		return false;
	}
}

/**
 * 创建目录
 *
 * @param $dir unknown_type
 *       	 路径
 * @param $mode unknown_type
 *       	 权限
 */
function makedir($dir, $mode = 0777) {
	if (! $dir)
		return 0;
	$dir = str_replace ( "\\", "/", $dir );
	
	$mdir = "";
	foreach ( explode ( "/", $dir ) as $val ) {
		$mdir .= $val . "/";
		if ($val == ".." || $val == ".")
			continue;
		
		if (! file_exists ( $mdir )) {
			if (! @mkdir ( $mdir, $mode )) {
				echo "创建目录 [" . $mdir . "]失败.";
				exit ();
			}
		}
	}
	return true;
}
/**
 * 生成缓存名字
 *
 * @param $name string
 *       	 原缓存名称
 * @return string $name 返回的缓存名称
 */
function getCacheName($name) {
	$name = md5 ( C ( 'cache_pre' ) . strtolower ( trim ( $name ) ) );
	return $name;
}
 
/**
 * 关键字加亮
 *
 * @param $str unknown_type       	
 * @param $key unknown_type       	
 */
function keyColor($str, $key) {
	foreach ( ( array ) $key as $val ) {
		$str = str_replace ( $val, '<span style="color:red">' . $val . '</span>', $str );
	}
	return $str;
}

/**
 * 写入文件
 *
 * @param $fileName string
 *       	 文件路径
 * @param $content string
 *       	 日志内容:不存在则读取;存在则保存
 */
function writeFile($fileName, $content = '') {
	$content1 = json_encode ( $content );
	$_path = explode ( '/', str_replace ( '\\', '/', $fileName ) );
	foreach ( ( array ) $_path as $k => $v ) {
		if ($k < count ( $_path ) - 1) {
			$strPath .= $v . '/';
		} else {
			$name = $v;
		}
	}
	$name = $_path [count ( $_path ) - 1];
	$path = str_replace ( '//', '/', SITE_PATH . '/data/HuaData/' . $strPath );
	makedir ( $path );
	$file = $path . $name;
	if ($content) {
		$fp = fopen ( $file, 'w+' );
		fwrite ( $fp, $content1 );
		fclose ( $fp );
	} else {
		$content1 = file_get_contents ( $file );
	}
	$rs = json_decode ( $content1 );
	return $rs;
}
/**
 * 函数作用，只替换一个字符
 *
 * @param $needle string
 *       	 需要替换的字符
 * @param $replace string
 *       	 替换成什么字符
 * @param $haystack string
 *       	 需要操作的字符串
 */
function strReplaceOnce($needle, $replace, $haystack) {
	$pos = strpos ( $haystack, $needle );
	if ($pos === false) {
		return $haystack;
	}
	return substr_replace ( $haystack, $replace, $pos, strlen ( $needle ) );
}

?>